package edu.cornell.cubooks.validator;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import edu.cornell.cubooks.domain.Feedback;
import edu.cornell.cubooks.utils.Santizer;

public class FeedbackValidator {
	public void validate(Feedback feedback, Errors errors) {
		
		feedback.setDescription(Santizer.santize(feedback.getDescription()));
		
		if(feedback.getRate() == null || feedback.getRate() < 1 || feedback.getRate() > 5){
			errors.rejectValue("rate", "feedback.rate.range");
		}
		
		if (!StringUtils.hasLength(feedback.getDescription().trim())) {
			errors.rejectValue("description", "feedback.description.required");
		}
	}
}
